//
//  ComboView.m
//  Project two framework
//
//  Created by rimi on 16/7/23.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ComboView.h"
#import "ComboViewCell.h"
#import "ComboViewFlowLayout.h"
#import "ZPZNetRequest+MealsList.h"
#import "ZPZNetRequest.h"
#import "LStringUtils.h"
#import "AFNetworking.h"
#import "ComboTableViewCell.h"

@interface ComboView ()<UICollectionViewDelegate,UICollectionViewDataSource,UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UICollectionView *collection;
@property(nonatomic,strong)NSMutableArray *array;
@property(nonatomic,strong)UIView *lietBtnView;
@property(nonatomic,strong)ComboViewFlowLayout *comboViewFlowLayout;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *tableArray;

@end


@implementation ComboView
-(instancetype)initWithFrame:(CGRect)frame{

    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor whiteColor];
         self.frame = CGRectMake(45, 120, 825, 580);
//        self.alpha = 0;
        [self collection];
//        [self tableView];
        [self initializeDataSource];
        
        
    }
    
    return self;
}
#pragma mark --集合视图数据源

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _array.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ComboViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellindext" forIndexPath:indexPath];
    cell.label.text = _array[indexPath.row][@"name"];
    NSArray *array = _array[indexPath.row][@"summaryFs"];
    if (array.count  >= 1) {
         cell.label2.text =  array[0][@"name"];
//        cell.label3.text = _array[indexPath.row][@"summaryFs"][1][@"name"];
    }
    if (array.count >= 2) {
        cell.label3.text = array[1][@"name"];
    }
    if (array.count >= 3) {
        cell.label4.text = array[2][@"name"];
    }
    
    
    return cell;
}

#pragma mark --tableView数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indextCell = @"ComboTableViewCell";
    ComboTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indextCell];
    if (cell == nil) {
        cell = [[ComboTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indextCell];
    }
    cell.substanceLabel.text = _array[indexPath.row][@"summaryFs"][@"name"];
    
    return cell;
}


#pragma mark --网络请求

-(void)initializeDataSource{
    [ZPZNetRequest getMealsListPageSize:@"10" PageNum:@"1" UserId:@"22" comletion:^(id object, BOOL success) {
//        NSLog(@"11111111111111%@",object);
        if (success) {
            _array = [NSMutableArray arrayWithArray:object[@"content"]];
            
            [_collection reloadData];
        }
        
       
        
    }];

}



-(UICollectionView *)collection{
    if (_collection == nil) {
//        //初始化一个布局
//        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
//        //设置间距
//        flowLayout.minimumLineSpacing = 60;
//        //滚动方向
//        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
//        //设置格子的大小
//        flowLayout.itemSize = CGSizeMake(335, 500);
        _comboViewFlowLayout = [[ComboViewFlowLayout alloc]init];
        
        //横向移动
        _comboViewFlowLayout.scrollDirection =UICollectionViewScrollDirectionHorizontal;
        _comboViewFlowLayout.itemSize = CGSizeMake(335, 500);
        
        
        _collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height) collectionViewLayout:_comboViewFlowLayout];
        _collection.backgroundColor = [UIColor clearColor];
//        _collection.
        //注册cell
        [_collection registerClass:[ComboViewCell class] forCellWithReuseIdentifier:@"cellindext"];
        _collection.delegate = self;
        _collection.dataSource = self;
        
//        [self initializeDataSource];
        [self addSubview:_collection];
    }



    return _collection;

}


//-(UIView *)lietBtnView
//{
//    if (_lietBtnView == nil) {
//        _lietBtnView = [[UIView alloc]initWithFrame:CGRectMake(800, 25, 50, 40)];
//        _lietBtnView.backgroundColor = [UIColor redColor];
//        [self addSubview:_lietBtnView];
//    }
//    return _lietBtnView;
//}

-(UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 90, 300, 420) style:UITableViewStylePlain];
        _tableView.rowHeight = 50;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
        [_collection addSubview:_tableView];
    }
    return _tableView;
}











@end
